/**
 * Created by Administrator on 2017/6/29.
 */
define(['knockout', 'jquery', 'echarts', 'echarts-theme-shine', 'director', 'bootstrap', 'platform.common', 'ikgMenu', 'ajaxSetup', 'accordion', 'fakeLoader'],
    function (ko, $) {
        var platform = require('platform.common');

        var ViewModel = function () {
            var self = this;
            self.router = Router({});

            self.uriTitle = ko.observable();
            self.currentHeight = ko.observable();
            self.curUser = ko.observable();
            self.appName = ko.observable('台区标杆值实测评估系统');
            self.companyRight = ko.observable('2017.山东安能信息技术有限公司.');
            self.loading = ko.observable(false);
            self.moduleOptions = ko.observable();

            self.init = function () {
                this.router.init();

                this.initUser();
                this.loadMenu();
                this.goToFront();
            };
            self.initUser = function () {
                var userInfo = platform.getCurrentUser();
                if (userInfo instanceof Object && userInfo.nick_name != undefined) {
                    self.curUser = ko.computed(function () {
                        return '欢迎,' + userInfo.nick_name;
                    }, self);
                }
            };
            self.loadMenu = function () {
                platform.servicebreakAsync('~/rest/loadMenu', {}, 'post').done(
                    function (response) {
                        if (response instanceof Object) {
                            $("#menuTree").ikgMenu({data: response}).on('nodeSelected',
                                function (event, data) {
                                    if (data.script_uri != '' || data.template_uri != '') {
                                        self.uriTitle(data.text);
                                        self.moduleOptions({
                                            name: data.script_uri,
                                            template: data.template_uri
                                        });
                                    }
                                }
                            );
                            $("#menuTree").jqueryAccordionMenu();
                        }
                    }
                );
            };
            self.logOut = function () {
                platform.servicebreakAsync("~/rest/shiroLogout").done(function (response) {
                    if (!response.responseObject.seprateMode) {
                        if (window.top !== window.self) {
                            window.top.location.href = response.responseObject.redirectUri;
                        } else {
                            window.parent.location = response.responseObject.redirectUri;
                        }
                    }
                });
            };

            self.goToFront = function () {//跳转到首页
                self.uriTitle('');
                self.moduleOptions({
                    name: 'module/theory/modules/front/index',
                    template: 'module/theory/templates/front/index'
                });
            }
        };
        return new ViewModel();
    });

$(function () {
    if (window.history && window.history.pushState) {
        $(window).on('popstate', function () {
            window.history.pushState('forward', null, '#');
            window.history.forward(1);
        });
    }
    window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
    window.history.forward(1);
});